var _pagination = null;
//页面缓存
var _cache = null;
//答案的缓存
var _answerCache = null;
// 当前操作题目Id
var _problemId = null;
/**
 * 入口，和服务器的通信从此开始
 */
$(function() {
	refreshInput($(".serWrapper"));
});

/**
 * 为查找做准备，这里主要是生成查询条件对象
 */
function toSearch() {
	var content = $("#searchContent").val();
	var type = $("#searchType").val();
	_pagination = new Pagination({
		url : '../problem/query',
		data : {
			content : content,
			type : type
		},
		success : printDataToPage,
		pageSize : 10
	});
}

function printDataToPage(message) {
	var response = new qmsk.Response(message);
	_cache = response;
	qmsk.dataTable({
		tbodyId : 'problemList',
		heads : ["typeName", "content", "createFileTime", ["problemId", "type"]],
		response : response,
		handler : [[3, detail]],
		sup : ['sup', '共 [index] 条']

	});
}

/**
 * 隐藏添加弹出层
 */
function hideAddPanel() {
	$("#window_change,#yinying1").hide();
}

function toAdd() {

	$("#problemContent").val('');

	$("#window_change,#yinying1").show();
	addMargin($("#window_change"));
	$("#addOrSaveButton").unbind('click');
	$("#addOrSaveButton").bind('click', add);
}

function toEdit(row) {

	var problemId = _cache.getValue('problemId', row);
	var content = _cache.getValue('content', row);
	var type = _cache.getValue('type', row);

	$("#problemContent").val(content);
	$("#problemType").val(type);

	$("#window_change,#yinying1").show();
	addMargin($("#window_change"));
	$("#addOrSaveButton").unbind('click');
	$("#addOrSaveButton").bind('click', {
		problemId : problemId
	}, edit);

}

function edit(event) {
	var problemId = event.data.problemId;

	var content = $("#problemContent").val();
	var type = $("#problemType").val();
	if (IsEmpty(content)) {
		artAlert("名称不能为空！");
		return false;
	}

	qmsk.post("../problem/update/" + problemId, {
		content : content,
		type : type
	}, function() {
		artSuccess("修改成功！");
		hideAddPanel();
		if (_pagination) {
			_pagination.reload();
		}
	});
}

function add() {

	var content = $("#problemContent").val();
	var type = $("#problemType").val();
	if (IsEmpty(content)) {
		artAlert("名称不能为空！");
		return false;
	}

	qmsk.post("../problem/insert", {
		content : content,
		type : type
	}, function() {
		artSuccess("添加成功！");
		hideAddPanel();
		if (_pagination) {
			_pagination.reload();
		}
	});
}

function detail(problemId, type, row) {
	var str = "<input class='edit common_btn' type='button' value='修改' onclick='toEdit(" + row + ")'>";
	if (type == 4) {
		str += "<input class='edit common_btn' type='button' value='配置' disabled = 'disabled'>";
	} else {
		str += "<input class='edit common_btn' type='button' value='配置'  onclick='configAnswer(" + problemId + ")'>";
	}
	return str;
}

function answerDetail(heads, row) {

	return "<input class='edit common_btn' type='button' value='修改' onclick='toUpdateAnswer(" + row + ")'>" + "<input class='edit common_btn del' type='button' value='删除' onclick='deleteAnswer(" + heads + ")'>";
}

function toAddAnswer() {
	$("#answerContent").val('');
	$("#answerAddUpdatePanel").show();
	$("#addOrSaveAnswerButton").unbind('click');
	$("#addOrSaveAnswerButton").bind('click', addAnswer);
}

function deleteAnswer(answerId) {
	qmsk.post('../answer/delete/' + answerId, function() {
		artSuccess("删除成功");
		loadAnswer();
	});
}

function addAnswer() {
	var content = $("#answerContent").val();
	qmsk.post('../answer/insert/' + _problemId, {
		content : content
	}, function(response) {
		artSuccess('添加成功！');
		closeAddOrUpdateAnswerPanel();
	});
}

function toUpdateAnswer(row) {
	var answerId = _answerCache.getValue('answerId', row);
	var content = _answerCache.getValue('content', row);
	$("#answerContent").val(content);
	$("#answerAddUpdatePanel").show();
	$("#addOrSaveAnswerButton").unbind('click');
	$("#addOrSaveAnswerButton").bind('click', {
		answerId : answerId
	}, updateAnswer);
}

function updateAnswer(event) {
	var answerId = event.data.answerId;
	var content = $("#answerContent").val();
	qmsk.post('../answer/update/' + answerId, {
		content : content
	}, function(response) {
		artSuccess('添加成功！');
		closeAddOrUpdateAnswerPanel();
	});
}

function closeAddOrUpdateAnswerPanel() {
	$("#answerAddUpdatePanel").hide();
	loadAnswer();
}

function loadAnswer() {
	qmsk.get('../answer/list/' + _problemId, function(response) {
		_answerCache = response;
		qmsk.dataTable({
			tbodyId : 'answerList',
			heads : ["answerId", "content", "answerId"],
			response : response,
			handler : [[2, answerDetail]],
			sup : ['anserSup', '共 [index] 条']
		});
	});
}

function configAnswer(problemId) {
	_problemId = problemId;
	loadAnswer();
	$("#answerPanel").show();
	$("#problemPanel").hide();
}

function closeAnswerPanel() {
	$("#answerPanel").hide();
	$("#problemPanel").show();
}
